home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2000 Spring / Oh!X 2000 Spring Special CD-ROM (Japan) (Part 2).7z / Oh!X 2000 Spring Special CD-ROM (Japan) (Part 2).bin / DXF / samples / multimedia / dplay / src / duel / lobby.cpp < prev    next >
C/C++ Source or Header  |  1999-07-23  |  9KB  |  241 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Lobby.cpp
  3. //
  4. // Desc: DP lobby related routines
  5. //
  6. // Copyright (C) 1995-1999 Microsoft Corporation. All Rights Reserved.
  7. //-----------------------------------------------------------------------------
  8. #include "duel.h"
  9. #include "lobby.h"
  10.  
  11.  
  12. //-----------------------------------------------------------------------------
  13. // Definitions
  14. //-----------------------------------------------------------------------------
  15. #define SAFE_DELETE(p)       { if(p) { delete (p);     (p)=NULL; } }
  16. #define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p);   (p)=NULL; } }
  17. #define SAFE_RELEASE(p)      { if(p) { (p)->Release(); (p)=NULL; } }
  18.  
  19. #define TIMERID         1           // Timer ID to use
  20. #define TIMERINTERVAL   250         // Timer interval
  21.  
  22.  
  23.  
  24.  
  25. //-----------------------------------------------------------------------------
  26. // Globals
  27. //-----------------------------------------------------------------------------
  28. extern LPDIRECTPLAY4      g_pDP;            // DirectPlay object pointer
  29. extern LPDPLCONNECTION    g_pDPLConnection; // Connection settings
  30. extern LPDIRECTPLAYLOBBY3 g_pDPLobby;       // Lobby object pointer
  31. extern BOOL               g_bHostPlayer;    // Flag indicating if we are hosting
  32. extern HWND               g_hwndMain;       // Main application window handle
  33. extern HINSTANCE          g_hInst;          // Application instance handle     
  34.  
  35. BOOL   g_bLobbyMsgSupported; // Lobby messages are supported
  36. GUID   g_guidPlayer;         // This player in this session
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. //-----------------------------------------------------------------------------
  44. // Name: DPLobbyRelease()
  45. // Desc: Wrapper for DirectPlayLobby Release API
  46. //-----------------------------------------------------------------------------
  47. HRESULT DPLobbyRelease()
  48. {
  49.     // Cleanup lobby
  50.     SAFE_DELETE_ARRAY( g_pDPLConnection );
  51.     SAFE_RELEASE( g_pDPLobby );
  52.  
  53.     return S_OK;
  54. }
  55.  
  56.  
  57.  
  58.  
  59. //-----------------------------------------------------------------------------
  60. // Name: DoingLobbyMessages()
  61. // Desc: Return TRUE if connected to a lobby and messages are supported.
  62. //-----------------------------------------------------------------------------
  63. BOOL DoingLobbyMessages()
  64. {
  65.     return( g_pDPLobby && g_bLobbyMsgSupported );
  66. }
  67.  
  68.  
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // Name: LobbyMessageInit()
  73. // Desc: Initialize lobby message processing.  Must be done while a lobby
  74. //       connection is open.
  75. //-----------------------------------------------------------------------------
  76. HRESULT LobbyMessageInit()
  77. {
  78.     if( NULL ==g_pDPLobby ) // Not connected to a lobby
  79.         return E_FAIL;
  80.  
  81.     g_bLobbyMsgSupported = FALSE; // Until we find out otherwise.
  82.     
  83.     // Find out if lobby messages are supported by the lobby
  84.     DPLMSG_GETPROPERTY msgGetProp;
  85.     ZeroMemory( &msgGetProp, sizeof(DPLMSG_GETPROPERTY) );
  86.     msgGetProp.dwType          = DPLSYS_GETPROPERTY;
  87.     msgGetProp.dwRequestID     = 1;                 // guidPlayer is not used
  88.     msgGetProp.guidPropertyTag = DPLPROPERTY_MessagesSupported;
  89.     
  90.     return g_pDPLobby->SendLobbyMessage( DPLMSG_STANDARD, 0, &msgGetProp,
  91.                                          sizeof(DPLMSG_GETPROPERTY) );
  92. }
  93.  
  94.  
  95.  
  96.  
  97. //-----------------------------------------------------------------------------
  98. // Name: LobbyMessageReceive()
  99. // Desc: Check for any lobby messages and handle them
  100. //       There are two modes:
  101. //          LMR_CONNECTIONSETTINGS - Waiting only for settings from a lobby
  102. //          LMR_PROPERTIES - Handle property message responses
  103. //-----------------------------------------------------------------------------
  104. HRESULT LobbyMessageReceive( DWORD dwMode )
  105. {
  106.     HRESULT hr = NOERROR;
  107.     LPVOID  lpMsgBuffer  = NULL;
  108.     DWORD   dwBufferSize = 0;
  109.     DWORD   dwRequiredSize;
  110.     DWORD   dwMsgFlags;
  111.  
  112.     if( NULL == g_pDPLobby )        // No lobby interface
  113.         return DPERR_NOINTERFACE;
  114.  
  115.     while( SUCCEEDED(hr) )          // Get all queued messages
  116.     {
  117.         dwRequiredSize = dwBufferSize;
  118.         hr = g_pDPLobby->ReceiveLobbyMessage( 0, 0, &dwMsgFlags,
  119.                                               lpMsgBuffer, &dwRequiredSize );
  120.         if( hr == DPERR_BUFFERTOOSMALL ) // Alloc msg buffer and try again
  121.         {
  122.             if( NULL == lpMsgBuffer )
  123.                 lpMsgBuffer = GlobalAllocPtr( GHND, dwRequiredSize );
  124.             else
  125.                 lpMsgBuffer = GlobalReAllocPtr( lpMsgBuffer, dwRequiredSize, 0 );
  126.  
  127.             if( NULL == lpMsgBuffer)
  128.                 return DPERR_NOMEMORY;
  129.  
  130.             dwBufferSize = dwRequiredSize;
  131.             hr = S_OK;
  132.         }
  133.         else if( SUCCEEDED(hr) && dwRequiredSize >= sizeof(DPLMSG_GENERIC) )
  134.         {
  135.             // Decode the message
  136.  
  137.             // Are we just looking for the CONNECTIONSETTINGS msg?
  138.             if( dwMode == LMR_CONNECTIONSETTINGS )
  139.             {
  140.                 if( (dwMsgFlags & DPLMSG_SYSTEM) &&
  141.                     ((DPLMSG_GENERIC*)lpMsgBuffer)->dwType == 
  142.                                                 DPLSYS_NEWCONNECTIONSETTINGS )
  143.                     break;
  144.                 else
  145.                     TRACE(_T("Non CONNECTIONSETTINGS lobby message ignored\n"));
  146.             }
  147.  
  148.             // Otherwise we handle only GetProperty responses
  149.             else if( (dwMsgFlags & DPLMSG_STANDARD) && 
  150.                      ((DPLMSG_GENERIC*)lpMsgBuffer)->dwType == 
  151.                                                     DPLSYS_GETPROPERTYRESPONSE )
  152.             {
  153.                 DPLMSG_GETPROPERTYRESPONSE* lpMsgGPR =
  154.                                     (DPLMSG_GETPROPERTYRESPONSE*)lpMsgBuffer;
  155.                 if( IsEqualGUID( lpMsgGPR->guidPropertyTag,
  156.                                  DPLPROPERTY_MessagesSupported ) )
  157.                 {
  158.                     if( (BOOL)lpMsgGPR->dwPropertyData[0] ) // Supported
  159.                     {
  160.                         // So request our player instance guid
  161.                         DPLMSG_GETPROPERTY msgGetProp;
  162.                         ZeroMemory(&msgGetProp, sizeof(DPLMSG_GETPROPERTY));
  163.                         msgGetProp.dwType      = DPLSYS_GETPROPERTY;
  164.                         msgGetProp.dwRequestID = 2;
  165.                         // guidPlayer is left NULL
  166.                         msgGetProp.guidPropertyTag = DPLPROPERTY_PlayerGuid;
  167.                         hr = g_pDPLobby->SendLobbyMessage( DPLMSG_STANDARD, 0,
  168.                                                 &msgGetProp,
  169.                                                 sizeof(DPLMSG_GETPROPERTY) );
  170.                         hr = S_OK;  // keep fetching messages
  171.                     }
  172.                     else    // not supported so close up shop
  173.                     {
  174.                         TRACE(_T("Lobby Messages not supported\n"));
  175.                         DPLobbyRelease();
  176.                         break;
  177.                     }
  178.                 }
  179.                 else if( IsEqualGUID( lpMsgGPR->guidPropertyTag,
  180.                                       DPLPROPERTY_PlayerGuid ) )
  181.                 {   
  182.                     // Have our player guid, ready to send property msgs
  183.                     g_guidPlayer = ( (DPLDATA_PLAYERGUID*)
  184.                                         &lpMsgGPR->dwPropertyData)->guidPlayer;
  185.                     g_bLobbyMsgSupported = TRUE;
  186.                 }
  187.             }
  188.             else
  189.                 TRACE(_T("Unrecognized lobby message ignored\n"));
  190.         }
  191.     }
  192.  
  193.     if( lpMsgBuffer )
  194.         GlobalFreePtr( lpMsgBuffer );
  195.  
  196.     return hr;
  197. }
  198.  
  199.  
  200.  
  201.  
  202. //-----------------------------------------------------------------------------
  203. // Name: LobbyMessageSetProperty()
  204. // Desc: Send a SetProperty message
  205. //-----------------------------------------------------------------------------
  206. HRESULT LobbyMessageSetProperty( const GUID* pPropTagGUID, VOID* pData,
  207.                                  DWORD dwDataSize )
  208. {
  209.     HRESULT hr;
  210.     LPBYTE  lpBuffer;
  211.     LPDPLMSG_SETPROPERTY lpMsgSP;
  212.     DWORD   dwMsgSize;
  213.  
  214.     if( NULL == g_pDPLobby )
  215.         return DPERR_NOCONNECTION;
  216.     if( NULL == g_bLobbyMsgSupported )
  217.         return DPERR_UNAVAILABLE;
  218.  
  219.     // Allocate and pack up the message
  220.     // Property data starts at dwPropertyData[0] for the size calculation
  221.     dwMsgSize = sizeof(DPLMSG_SETPROPERTY) - sizeof(DWORD) + dwDataSize;
  222.     lpBuffer = (LPBYTE)GlobalAllocPtr(GHND, dwMsgSize);
  223.     if( NULL == lpBuffer )
  224.         return (DPERR_NOMEMORY);
  225.     lpMsgSP                  = (LPDPLMSG_SETPROPERTY)lpBuffer;
  226.     lpMsgSP->dwType          = DPLSYS_SETPROPERTY;
  227.     lpMsgSP->dwRequestID     = DPL_NOCONFIRMATION;
  228.     lpMsgSP->guidPlayer      = g_guidPlayer;            // player property assumed
  229.     lpMsgSP->guidPropertyTag = (*pPropTagGUID);
  230.     lpMsgSP->dwDataSize      = dwDataSize;
  231.     memcpy( lpMsgSP->dwPropertyData, pData, dwDataSize );
  232.     hr = g_pDPLobby->SendLobbyMessage( DPLMSG_STANDARD, 0, lpBuffer, dwMsgSize );
  233.     
  234.     GlobalFreePtr( lpBuffer );
  235.  
  236.     return hr;
  237. }
  238.  
  239.  
  240.  
  241.